var urlPrefix = "https://bird.ioliu.cn/v1?url=",
	imgPrefix = "http://hospital.xiaoweicanting.com/";
mui.plusReady(function() {
	var cv = plus.webview.currentWebview();
	mui.ajax({
		url: urlPrefix + "http://hospital.xiaoweicanting.com/factory.php?m=App&c=engineer&a=getEngineerList&factoryid=1&uid=3&token=dfaf",
		type: "get",
		dataType: "json",
		async: true,
		success: function(response) {
			if(response.state === "success") {
				var data = response.data,
					html = "";
				if(data.length) {
					html += '<ul class="mui-table-view">';
					for(var i = 0, len = data.length; i < len; i++) {
						html += '<li class="mui-table-view-cell" data-factory="' + data[i].factoryid + '" data-engineer=\'{"id":' + data[i].eid + ',"name":"' + data[i].username + '"}\'>' + 
								 	'<p class="display-container">' + 
								 		'<i class="engineer-display" style="background-image: url(' + imgPrefix + data[i].headimg + ')"></i>' + 
								 		'<span class="engineer-name">' + data[i].username + '</span>' + 
								 	'</p>' + 
								 	'<p class="state-container">' + 
								 		'<span class="engineer-state ' + (data[i].busy ? 'busy-state">繁忙</span><span class="mui-badge mui-badge-warning">' + data[i].ordernum : 'idle-state">空闲') + '</span>' + 
								 	'</p>' + 
							 		'<ul class="engineer-info">' + 
							 			'<li>电话：' + data[i].phone + '</li>' + 
							 			'<li>厂家：' + data[i].factoryname + ' 地点：' + data[i].location + '</li>' + 
							 			'<li>职位：' + data[i].role + '</li>' + 
								 	'</ul>' + 
								 '</li>';
					}
					html += '</ul>';
				}
				var engineerList = document.getElementById("engineer-list");
				if(html) engineerList.innerHTML = html;
				[].slice.call(engineerList.querySelectorAll(".mui-table-view-cell")).forEach(function(v) {
					v.addEventListener("tap", function() {
						var self = this, btnArray = ['否', '是'];
						var engineerInfo = JSON.parse(this.getAttribute("data-engineer"));
						mui.confirm("您确定要把此工单分配给" + engineerInfo.name + "吗？", "分配工单", btnArray, function(e) {
							if(e.index == 1) {
								mui.openWindow({
									url: "dispatchTask.html",
									extras: {
										factoryId: self.getAttribute("data-factory"),
										eid: engineerInfo.id,
										sid: cv.sid
									}
								})
							}
						})
					}, false);
				})
			}
		}
	});
})


